/**
 * 日期指令封装
 */
import tem from './index.html'
import $_fn from '~plugins'
import '../../../assets/css/datepicker.less'
class uidtepickerCtrl {
    constructor($scope,$rootScope,$location,$interval,$state,uibDateParser) {
        $scope.opened = false
        $scope.format = 'yyyy-MM-dd';
    //    $scope.time = new Date($scope.ngModel);
    //   if($scope.ngModel && typeof($scope.ngModel) != 'object' ){
    //       $scope.time = new Date($scope.ngModel);
    //   }
        
        $scope.altInputFormats = ['yyyy-MM-dd'];
        $scope.placeholderVal = $scope.placeholder
        //这里可以配置很多
        $scope.dateOptions = {
            formatYear: 'yy',
            // maxDate: new Date(2020, 5, 22),
            // minDate: new Date(),
            startingDay: 1,
            showWeeks:false
        };
        $scope.open = function () {
            $scope.opened = true
            $scope.errorTis = false
        }
      $scope.$watch("ngModel",function(newVal, oldVal){
        var ngModel = uibDateParser.filter(newVal,'yyyy-MM-dd');
        if(newVal !== oldVal){
            $scope.time = typeof(newVal) == 'string'?new Date(newVal):typeof(newVal) == 'object'?newVal:ngModel;
        }
        console.log('watch1',$scope.time)
        
      })
      $scope.$watch("time",function(newVal, oldVal){
        // console.log('watch2',newVal)
        if(newVal !== oldVal){
            $scope.ngModel = uibDateParser.filter(newVal,'yyyy-MM-dd');
        }
        })
        $scope.saveDate()
        
    }
  
}


uidtepickerCtrl.$inject = ['$scope','$rootScope','$location','$interval','$state','uibDateParser']
module.exports = angular.module('directive.uiDatepicker', []).directive('uiDatepicker', function() {
    return {
        template: tem,
        replace:true,
        scope:{
            placeholder:'@',
            errorTis:'<',
            ngModel:"=",
            saveDate:"&"
        },
        controller: uidtepickerCtrl
    }
}).name